package friend

import (
	"api/common"
	"api/pb/friend"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateApplyLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateApplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateApplyLogic {
	return &CreateApplyLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// CreateApply 新增好友申请
func (l *CreateApplyLogic) CreateApply(req *types.ApplyInfoRequest) (resp *types.Empty, err error) {
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}

	_, err = l.svcCtx.FriendRPC.AddApply(l.ctx, &friend.ApplyInfo{
		FromUserId: userID,
		ToUserId:   req.ToUserID,
		Message:    req.Message,
		Status:     common.FriendApplyStatusWait,
	})
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
